var fs = require('fs');
var join = require('path').join;

function outputJs(dataName,jsonObj) {
    var fileName = dataName+'Cfg';

    var content = "var HashMap = require('HashMap')\n";
    content += "var "+fileName+" = function () { \r";
    if (Array.isArray(jsonObj)){
        content += "    this.dataMap = new HashMap();\r";
    }else{
        var keys = Object.keys(jsonObj);
        for (var i=0;i<keys.length;i++){
            var key = keys[i];
            if (!!key){
                content+="    this."+key+" = "+jsonObj[key]+";\r"
            }
        }
    }
    content += "}\r";
    content += "var pro = "+fileName+".prototype;\r";
    content += "pro.initWithData = function(data){\r";
    if (Array.isArray(jsonObj)){
        content += "    for (var i=0;i<data.length;i++){\r";
        content += "        var itemTmp = data[i];\r";
        content += "        var item = create"+dataName+"(itemTmp);\r";
        content += "        this.dataMap.put(item.id,item)\r";
        content += "    }\r";
    }
    content += "}\r";
    //get方法
    content += "pro.get"+dataName+"WithId = function(dataId){\r";
    content += "    if(this.dataMap.hasKey(dataId)){\r";
    content += "        return this.dataMap.get(dataId)\r";
    content += "    }\r";
    content += "}\r";
    //has方法
    content += "pro.has"+dataName+" = function(dataId){\r";
    content += "    return this.dataMap.hasKey(dataId)\r";
    content += "}\r";

    if (Array.isArray(jsonObj)){
        content += "var create"+dataName+" = function(item){\r";
        content += "    var obj = {}\r";
        var itemObj = jsonObj[0];
        if (!itemObj){
            itemObj = {};
        }
        var keys = Object.keys(itemObj);
        for (var i=0;i<keys.length;i++){
            var key = keys[i];
            if (!!key){
                content+="    obj."+key+" = item."+key+";\r"
            }
        }
        content += "    return obj\r";
        content += "}\r";
    }
    content += "module.exports = new "+fileName+"();\r"
    fs.writeFileSync('meta/'+fileName+'.js',content);
}

function loadJsonFiles(path){
    var content = "module.exports = {\n";

    var fileOuput = {};
    let files = fs.readdirSync(path);

    files.forEach(function (item, index) {
        let fPath = join(path,item);
        let stat = fs.statSync(fPath);
        // if(stat.isDirectory() === true) {
        //     loadJsonFiles(fPath);
        // }
        if (stat.isFile() === true && item.indexOf('.json') > 0) {
            var fileName = item.replace('.json','');
            var jsonObj = JSON.parse(fs.readFileSync(fPath));

            fileOuput[fileName] = jsonObj;
            outputJs(fileName,jsonObj);

            content += "    "+fileName+":require('"+fileName+"Cfg'),\r";
        }
    });
    fs.writeFileSync('output/meta.json', JSON.stringify(fileOuput));

    content += "}\r";
    fs.writeFileSync('metaCfg.js',content);
}
var excelToJson = require('./excelToJson');
excelToJson.loadExcel('excel','meta_data');
loadJsonFiles('meta_data');


var exec = require('child_process').exec;
function execute(){
    var cmd='cp -rf meta/* ../assets/Script/meta' +
        '&&cp -rf output/* ../assets/resources/config';
    exec(cmd, function(error, stdout, stderr) {
        console.log(stdout);
        if(error){
            console.log(error);
        }
        else{
            console.log("数据已更新");
        }
    });
}
execute();